package com.sz.biz.logistics.ord.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.trade.constants.TrdConstants;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.biz.logistics.bill.dto.BillingDto;
import com.sz.biz.logistics.bill.dto.BillingGenerateDto;
import com.sz.biz.logistics.ord.entity.OrdServiceBill;
import com.sz.biz.logistics.ord.service.OrdServiceBillService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Function: 服务计费 <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-03-31 13:53:00.
 */
@Service("ordServiceBillService")
public class OrdServiceBillServiceImpl extends AbstractService implements OrdServiceBillService {
    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.logistics.ord.entity.mapper.OrdServiceBillMapper";
    }

    @Override
    public void save(OrdServiceBill entry) {
        dao.save(getSqlName("insertSelective"), entry);
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public void batchdelete(List<Integer> ids) {

    }

    @Override
    public void batchUpdate(List<OrdServiceBill> list) {

    }

    @Override
    public int update(OrdServiceBill entry) {
        return 0;
    }

    @Override
    public OrdServiceBill findById(Integer id) {
        return null;
    }

    @Override
    public List<OrdServiceBill> findByIds(List<Integer> ids) {
        return null;
    }

    @Override
    public QResultDto findByParam(QueryParams params) {
        return null;
    }

    @Override
    public void checkCode(int sid, String code) {

    }


    /**
     * 子帐号账单管理添加接口
     * 根据帐号id，已收获时间段获取订单
     *
     * @param customerId
     * @param startEffectTime
     * @param endEffectTime
     * @param isBilled
     * @return
     */
    public List<BillingGenerateDto> getOrderByUserIdAndCreateTime(Integer customerId, Date startEffectTime, Date endEffectTime, Boolean isBilled) {
        ParamData paramData = new ParamData();
        paramData.put("userId", customerId);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("isBilled", isBilled);

        return dao.findForList(getSqlName("getOrderByUserIdAndCreateTime"), paramData, BillingGenerateDto.class);
    }

    /**
     * 没有汇率的订单
     *
     * @param customerIds
     * @param startEffectTime
     * @param endEffectTime
     * @return
     */
    @Override
    public List<OrdServiceBill> getOrderNotExchangeRateByCustomerIdAndCreateTime(List<Integer> customerIds, Date startEffectTime, Date endEffectTime, String type) {
        ParamData paramData = new ParamData();
        paramData.put("customerIds", customerIds);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("type", type);
        if (TrdConstants.BillType.PAYABLE.equals(type)) {
            return dao.findForList(getSqlName("getOrderNotExchangeRateByCustomerAndCreateTime"), paramData, OrdServiceBill.class);
        } else {
            return dao.findForList(getSqlName("getOrderNotExchangeRateBySupplierIdAndCreateTime"), paramData, OrdServiceBill.class);
        }
    }

    /**
     * 获取订单数量
     *
     * @param customerIds
     * @param startEffectTime
     * @param endEffectTime
     * @param isBilled
     * @return
     */
    @Override
    public List<BillingGenerateDto> getOrderForBillingTask(List<Integer> customerIds, Date startEffectTime, Date endEffectTime, Boolean isBilled, String type) {
        List<BillingGenerateDto> billingGenerateDtos = new ArrayList<BillingGenerateDto>();
        ParamData paramData = new ParamData();
        paramData.put("customerIds", customerIds);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("isBilled", isBilled);
        paramData.put("type", type);
        if (TrdConstants.BillType.PAYABLE.equals(type)) {
            billingGenerateDtos = dao.findForList(getSqlName("getOrderForByCustomerIdBillingTask"), paramData, BillingGenerateDto.class);
        } else {
            billingGenerateDtos = dao.findForList(getSqlName("getOrderForBySupplierIdBillingTask"), paramData, BillingGenerateDto.class);
        }
        return billingGenerateDtos;
    }

    /**
     * 更新是否已经生成账单状态
     *
     * @param orderNos
     * @param isBilled
     */
    @Override
    public void updateIsBilledStatus(List<String> orderNos, Boolean isBilled, String type, Integer bizId) {
        if (null != orderNos && orderNos.size() > 0) {
            ParamData paramData = new ParamData();
            paramData.put("orderNos", orderNos);
            paramData.put("isBilled", isBilled);
            paramData.put("type", type);
            paramData.put("bizId", bizId);
            dao.update(getSqlName("updateIsBilledStatus"), paramData);
        }
    }

    /**
     * 删除某个订单的账单
     *
     * @param orderNo
     */
    @Override
    public void deleteByOrderNo(String orderNo) {
        dao.delete(getSqlName("deleteByOrderNo"), orderNo);
    }

    /**
     * 更新
     *
     * @param ordServiceBill
     */
    @Override
    public void updateExchangeRate(OrdServiceBill ordServiceBill) {
        dao.update(getSqlName("updateExchangeRate"), ordServiceBill);
    }

    /**
     * 获取没有汇率的单
     *
     * @return
     */
    @Override
    public List<OrdServiceBill> findExchangeRateEmpty() {
        return dao.findForList(getSqlName("findExchangeRateEmpty"), null, OrdServiceBill.class);
    }

    /**
     * 按单号获取计算出来的值
     *
     * @param customerId customerId
     * @param orderNos   orderNos
     * @return List<OrdServiceBill>
     */
    @Override
    public List<OrdServiceBill> findByOrderNos(Integer customerId, List<String> orderNos) {
        if (customerId == null || customerId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "customerId");
        }
        if (CollectionUtils.isEmpty(orderNos)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "orderNos");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("type", 2);
        map.put("customerId", customerId);
        map.put("orderNos", orderNos);
        return dao.findForList(getSqlName("findByOrderNos"), map, OrdServiceBill.class);
    }

    @Override
    public BillingDto preGenerateCustomerBilling(List<Integer> customerIds, Date startEffectTime, Date endEffectTime, String type) {
        ParamData paramData = new ParamData();
        paramData.put("customerIds", customerIds);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("type", type);
        return (BillingDto) dao.findForObject(getSqlName("preGenerateCustomerBilling"), paramData);
    }

    @Override
    public BillingDto preGenerateSupplierBilling(List<Integer> customerIds, Date startEffectTime, Date endEffectTime, String type) {
        ParamData paramData = new ParamData();
        if (customerIds != null && customerIds.size() > 0) {
            paramData.put("customerIds", customerIds);
        }
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("type", type);
        return (BillingDto) dao.findForObject(getSqlName("preGenerateSupplierBilling"), paramData);
    }

    /**
     * 账单管理添加接口
     * 根据客户id，已收获时间段获取订单
     *
     * @param customerId
     * @param startEffectTime
     * @param endEffectTime
     * @param isBilled
     * @return
     */
    @Override
    public List<BillingGenerateDto> getOrderByCustomerAndCreateTime(Integer customerId, Date startEffectTime, Date endEffectTime, Boolean isBilled) {
        ParamData paramData = new ParamData();
        paramData.put("customerId", customerId);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("isBilled", isBilled);
        return dao.findForList(getSqlName("getOrderByCustomerAndCreateTime"), paramData, BillingGenerateDto.class);
    }

    /**
     * 账单管理添加接口
     * 根据供应商id，已收获时间段获取订单
     *
     * @param customerId
     * @param startEffectTime
     * @param endEffectTime
     * @param isBilled
     * @return
     */
    @Override
    public List<BillingGenerateDto> getOrderBySupplierAndCreateTime(Integer customerId, Date startEffectTime, Date endEffectTime, Boolean isBilled) {
        ParamData paramData = new ParamData();
        paramData.put("customerId", customerId);
        paramData.put("startEffectTime", startEffectTime);
        paramData.put("endEffectTime", endEffectTime);
        paramData.put("isBilled", isBilled);
        return dao.findForList(getSqlName("getOrderBySupplierAndCreateTime"), paramData, BillingGenerateDto.class);
    }

    /**
     * 按照日期获取计费时间内的订单
     *
     * @param type
     * @param dayBegin
     * @param dayEnd
     */
    @Override
    public List<OrdServiceBill> findByDate(int type, Date dayBegin, Date dayEnd) {
        ParamData paramData = new ParamData();
        paramData.put("type", type);
        paramData.put("startTime", dayBegin);
        paramData.put("endTime", dayEnd);
        return dao.findForList(getSqlName("getBillByDate"), paramData, OrdServiceBill.class);
    }


    /**
     * 根据单号和类型查询
     *
     * @param orderNo orderNo
     * @param type    1采购价 2销售价
     * @return
     */
    @Override
    public List<OrdServiceBill> findByOrderNoAndType(String orderNo, int type) {
        ParamData paramData = new ParamData();
        paramData.put("type", type);
        paramData.put("orderNo", orderNo);
        return dao.findForList(getSqlName("findByOrderNoAndType"), paramData, OrdServiceBill.class);
    }

    /**
     * 根据单号和类型删除
     *
     * @param orderNo orderNo
     * @param type    1采购价 2销售价
     * @return
     */
    @Override
    public void deleteByOrderNoAndType(String orderNo, int type) {
        ParamData paramData = new ParamData();
        paramData.put("type", type);
        paramData.put("orderNo", orderNo);
        dao.delete(getSqlName("deleteByOrderNoAndType"), paramData);
    }

    /**
     * 根据订单号集合查询
     *
     * @param orderNos 订单集合
     * @param customerId 客户id
     * @return OrdServiceBills 费用明细集合
     * @author Fengwj
     * @date 2017/12/27
     */
    @Override
    public List<OrdServiceBill> getByOrderNos(List<String> orderNos, Integer customerId) {
        if (CollectionUtils.isEmpty(orderNos)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "orderNos");
        }
        if (null == customerId || customerId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "customerId");
        }
        ParamData paramData = new ParamData();
        paramData.put("orderNos", orderNos);
        paramData.put("customerId", customerId);
        return dao.findForList(getSqlName("findDetailFeeByOrderNos"), paramData, OrdServiceBill.class);
    }
}
